import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LC39
 * 组合总和
 * @Author ykx
 * @Date 2024/3/15 9:51
 * @Version 1.0
 */
public class LC39 {
    public static void main(String[] args) {
        LC39 lc39 = new LC39();
        int[] candidates = new int[]{2, 3, 6, 7};
        int target = 7;
        System.out.println(lc39.combinationSum(candidates, target));
    }

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        // 结果
        List<List<Integer>> result = new ArrayList<>();

        // 每个元素
        List<Integer> integers = new ArrayList<>();

        combination(0, result, integers, candidates, target);

        return result;
    }

    public void combination(int start, List<List<Integer>> result, List<Integer> integers, int[] candidates, int target) {
        if (target == 0) {
            result.add(new ArrayList<>(integers));
            return;
        }
        for (int i = start; i < candidates.length; ++i) {
            if (candidates[i] <= target) {
                integers.add(candidates[i]);
                combination(i, result, integers, candidates, target - candidates[i]);
                integers.remove(integers.size() - 1);
            }
        }
    }


}
